﻿using ProjScan.GUIExtensions;
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace ProjScan.ScanGUI
{
    public class ScanOverviewGUI
    {
        private GUITableState tableState;

        private float lastWindowWidth;

        private ScanOverviewData scanOverviewData;

        public bool IsShowScanResultsSetting
        {
            get;
            set;
        }

        public ScanOverviewGUI(ScanOverviewData scanOverviewData)
        {
            this.scanOverviewData = scanOverviewData;
            IsShowScanResultsSetting = true;
            tableState = new GUITableState();
        }

        private void DrawProgressBar(float value)
        {
            Rect rect = GUILayoutUtility.GetRect(100f, 20f);
            float num = rect.y + rect.height / 2f;
            float num2 = 10f;
            float num4 = rect.y = num - num2 / 2f;
            rect.height = num2;
            float num5 = rect.width * value;
            float width = rect.width - num5;
            Rect rect2 = new Rect(rect.x, rect.y, num5, rect.height);
            EditorGUI.DrawRect(rect2, Color.green);
            Rect rect3 = new Rect(rect.x + num5, rect.y, width, rect.height);
            EditorGUI.DrawRect(rect3, Color.red);
            Rect position = new Rect(rect.x, rect.y, rect.width, rect.height);
            string label = $"{value * 100f}%";
            EditorGUI.LabelField(position, label, new GUIStyle
            {
                alignment = TextAnchor.MiddleCenter
            });
        }

        private void DrawScanResultsInfo(float width)
        {
            if (Math.Abs(width - lastWindowWidth) > float.Epsilon)
            {
                tableState = new GUITableState();
                lastWindowWidth = width;
            }

            float width2 = width / 4f;
            List<TableColumn> columns = new List<TableColumn>
            {
                new TableColumn("", width2),
                new TableColumn("", width2),
                new TableColumn("", width2),
                new TableColumn("", width2)
            };
            int percentageFromScanData = GetPercentageFromScanData(scanOverviewData.BasicAssetsCheck);
            int percentageFromScanData2 = GetPercentageFromScanData(scanOverviewData.ScenesCheck);
            int percentageFromScanData3 = GetPercentageFromScanData(scanOverviewData.GlobalSettingsCheck);
            int percentageFromScanData4 = GetPercentageFromScanData(scanOverviewData.CSCheck);
            int percentageFromScanData5 = GetPercentageFromScanData(scanOverviewData.EffectsPlayCheck);
            int percentageFromScanData6 = GetPercentageFromScanData(scanOverviewData.ShaderAnalysis);
            int percentageFromScanData7 = GetPercentageFromScanData(scanOverviewData.ArtAssetsCheck);
            List<List<TableEntry>> list = new List<List<TableEntry>>();
            for (int i = 0; i < 1; i++)
            {
                list.Add(new List<TableEntry>
                {
                    new LabelEntry("版本号: " + scanOverviewData.Version),
                    new LabelEntry($"基本资源检测: {percentageFromScanData}%  ({scanOverviewData.BasicAssetsCheck})"),
                    new LabelEntry($"场景检测: {percentageFromScanData2}%  ({scanOverviewData.ScenesCheck})"),
                    new LabelEntry($"Editor设置检测: {percentageFromScanData3}%  ({scanOverviewData.GlobalSettingsCheck})")
                });
                list.Add(new List<TableEntry>
                {
                    new LabelEntry($"C#代码扫描检测: {percentageFromScanData4}%  ({scanOverviewData.CSCheck})"),
                    new LabelEntry($"特效播放检测: {percentageFromScanData5}%  ({scanOverviewData.EffectsPlayCheck})"),
                    new LabelEntry($"Shader分析: {percentageFromScanData6}%  ({scanOverviewData.ShaderAnalysis})"),
                    new LabelEntry($"美术资源检测: {percentageFromScanData7}%  ({scanOverviewData.ArtAssetsCheck})")
                });
            }

            tableState = GUITable.DrawTable(columns, list, tableState, hideHeader: true, scrollView: false, 30f);
        }

        public void DrawGUI(float width)
        {
            GUIStyle foldoutHeader = EditorStyles.foldoutHeader;
            EditorGUILayout.BeginHorizontal();
            string content = "时间: " + scanOverviewData.Time + ",    " + $"规则数：{GetPercentageFromScanData(scanOverviewData.Overall)}%" + "  (" + scanOverviewData.Overall + "),   " + $" 优先级分数: {GetPercentageFromScanData(scanOverviewData.ScoreByRulePriority)}%" + "  (" + scanOverviewData.ScoreByRulePriority + ")";
            IsShowScanResultsSetting = EditorGUILayout.BeginFoldoutHeaderGroup(IsShowScanResultsSetting, content, foldoutHeader);
            GUILayout.FlexibleSpace();
            float value = (float)GetPercentageFromScanData(scanOverviewData.ScoreByRulePriority) / 100f;
            DrawProgressBar(value);
            EditorGUILayout.EndHorizontal();
            if (IsShowScanResultsSetting)
            {
                EditorGUILayout.BeginHorizontal();
                int num = 20;
                GUILayout.Space(num);
                using (new EditorGUILayout.VerticalScope(EditorStyles.helpBox))
                {
                    DrawScanResultsInfo(width - (float)(num * 3));
                }

                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndFoldoutHeaderGroup();
        }

        private int GetPercentageFromScanData(string scanData)
        {
            string[] array = scanData.Split(new char[1]
            {
                '/'
            });
            float num = float.Parse(array[0]);
            float num2 = float.Parse(array[1]);
            if (num * num2 == 0f || num == -1f || num2 == -1f)
            {
                return 0;
            }

            return (int)((double)num / (double)num2 * 100.0);
        }
    }
}